"""Some parts taken or based on www.devshed.com/c/a/Python/PyGame-for-Game-Development-Font-and-Sprites/3/"""
"""Some parts take or based on http://www.linuxjournal.com/article/7694"""

import pygame, os

SCREENDIM = pygame.Rect(0, 0, 800, 800)


class Screen:
    def __init__(self):
        self.window = self.load_screen()
        pass
    
    """Gets the image and converts it into a format that is quick to run,
    and it returns the image and the rectangular object containing it."""
    def load_image(self, file_name):
        full_file_name = os.path.join("images", file_name)
        try:
            self.image = pygame.image.load(full_file_name)
        except (pygame.error):
            print("Cannot load image:", full_file_name)
            raise (SystemExit)
        
        self.image.set_colorkey((255, 255, 255))
        self.image = self.image.convert()
        return self.image, self.image.get_rect()
    
    #Creates a Surface that will serve as the game window
    def load_screen(self):
        pygame.init()
    
        self.Width = SCREENDIM.width
        self.Height = SCREENDIM.height
        self.Size = self.Width, self.Height
        
        #Creates the GUI window with caption and load the background.
        self.screen = pygame.display.set_mode(self.Size)
        pygame.display.set_caption("Space Control")
        
        return self.screen
    
    def updateDisplay(self):
        pygame.display.update()
        return True

###########################################################################
#Map Class, to hold information about the background
#
#mapSize - size of the map, which should be square,
#          and it should be divisible by 1500, for territories,
#          and by 3000, for difficulty increases
class Map():
    def __init__(self, screen, mapSize=(12000, 12000), terrSize=1500):
        self.background_image, self.background_rect = screen.load_image("starfield.bmp")
        self.mapSize = mapSize
        
        #May need this later
        self.mapLayout = (
                    (1, 1, 1),
                    (1, 1, 1),
                    (1, 1, 1)
                    )
        
        self.terrSize = terrSize
        self.init_Territories()
    
    #Redraw map based on ship's position
    def update(self, screen, position):
        #You're obviously going to print the grid you're currently in
        currentGridLoc = (SCREENDIM.width / 2 - position[0] % 500, SCREENDIM.height / 2 - position[1] % 500)
        #Center
        screen.blit(self.background_image, currentGridLoc)
        
        #Now print the surrounding grids
        #Left
        screen.blit(self.background_image, (currentGridLoc[0] - 500, currentGridLoc[1]))
        #Top Left
        screen.blit(self.background_image, (currentGridLoc[0] - 500, currentGridLoc[1] - 500))
        #Top
        screen.blit(self.background_image, (currentGridLoc[0], currentGridLoc[1] - 500))
        #Top Right
        screen.blit(self.background_image, (currentGridLoc[0] + 500, currentGridLoc[1] - 500))
        #Right
        screen.blit(self.background_image, (currentGridLoc[0] + 500, currentGridLoc[1]))
        #Bottom Right
        screen.blit(self.background_image, (currentGridLoc[0] + 500, currentGridLoc[1] + 500))
        #Bottom
        screen.blit(self.background_image, (currentGridLoc[0], currentGridLoc[1] + 500))
        #Bottom Left
        screen.blit(self.background_image, (currentGridLoc[0] - 500, currentGridLoc[1] + 500))
        
        return True
    
    def init_Territories(self):
        numTerrOneDim = int(self.mapSize[0] / self.terrSize)
        #Create two-dimensional array
        self.terr = [[None for y in range(0, numTerrOneDim)] for x in range(0, numTerrOneDim)] #@UnusedVariable
        return True
